var GeoUtils = {};

GeoUtils.isPointInPolygon = function (point, polygon) {
    if(!point || point.length<2) {
        return false
    }
    if(!polygon || polygon.length<4) {
        return false
    }
    
    var nCross = 0;
    var n = polygon.length;
    for(let i=0; i<n; i++) {
        var p1 = polygon[i];
        var p2 = polygon[(i+1)%n];
        if(Number(p1.longitude) == Number(p2.longitude)) continue;
        
        if(Number(point[0]) < Math.min(Number(p1.longitude), Number(p2.longitude))) continue;
        
        if(Number(point[0]) >= Math.max(Number(p1.longitude), Number(p2.longitude))) continue;
        
        var y = (Number(point[0]) - Number(p1.longitude)) * (Number(p2.latitude) - Number(p1.latitude)) / (Number(p2.longitude) - Number(p1.longitude)) + (Number(p1.latitude))
        
        if(y > Number(point[1])) nCross++;
    }
    return (nCross%2 === 1)
}
module.exports = {
    GeoUtils
}